import java.awt.CardLayout;

import javax.swing.*;


public class MainFrame extends JFrame {
	ConnectGTD connect = new ConnectGTD();
	CardLayout c = new CardLayout(0, 0);

	AddUserGUI au = new AddUserGUI();
	EditPatientGUI edit = new EditPatientGUI();
	LoginGUI login = new LoginGUI();
	DoctorGUI doctor = new DoctorGUI();
	NurseGUI nurse = new NurseGUI();
	AdminGUI admin = new AdminGUI();
	DoctorsOrdersGUI Do = new DoctorsOrdersGUI();
	HealthHistoryGUI hh = new HealthHistoryGUI();
	SearchPatientGUI sp = new SearchPatientGUI();
	NewPatientGUI np = new NewPatientGUI();
	TreatmentRecordGUI tr = new TreatmentRecordGUI();
	ViewAppointmentsGUI va = new ViewAppointmentsGUI();
	SearchUserGUI su = new SearchUserGUI();
	EditUserGUI eu = new EditUserGUI();
	PatientGUI pg = new PatientGUI();
	SearchAppointments sa = new SearchAppointments();
	InputDoctorsOrdersGUI id = new InputDoctorsOrdersGUI();
	SearchTRGui str = new SearchTRGui();

	int role = 0;

	public MainFrame() {

		getContentPane().setLayout(c);

		getContentPane().add("login", login);
		getContentPane().add("doctor", doctor);
		getContentPane().add("nurse", nurse);
		getContentPane().add("admin", admin);
		getContentPane().add("Do", Do);
		getContentPane().add("hh", hh);
		getContentPane().add("sp", sp);
		getContentPane().add("EditP", edit);
		getContentPane().add("np", np);
		getContentPane().add("tr", tr);
		getContentPane().add("va", va);
		getContentPane().add("su", su);
		getContentPane().add("eu", eu);
		getContentPane().add("pg", pg);
		getContentPane().add("sa", sa);
		getContentPane().add("id", id);
		getContentPane().add("au", au);
		getContentPane().add("hh", hh);
		getContentPane().add("str", str);

		// cards = new JPanel(cl);
		// cards.add(login);
		// cards.add(doctor);

		// cards.remove(login);

		// cl.show(cards, "doctor");

		// this.add(login);
		// this.add(nurse);
		// this.add(doctor);
		// this.add(admin);
		// this.add(edit);

	}

	public void changeScreen() {

		//System.out.println("ChangeScreen");
		if (pg.getLoc() == 14) {
			sa.setUser(connect.getCurrentUser());
			c.show(getContentPane(), "sa");
		}else if (login.getRole() == "patient" && login.getLoc() == 1) {
			connect.setUsername(login.name());
			connect.setPassWord(login.pass());
			connect.setCurrentUser(connect.login(login.getRole()));
			System.out.println(connect.getCurrentUser());
			/*if (connect.getUser().getRoles()[0] == true)*/
			c.show(getContentPane(), "pg");
		}else if (eu.getLoc() == 3) {
			connect.setUser(eu.getUser());
			connect.editUser();
			eu.setLoc(-1);
		}else if (doctor.getLoc() == 10 || nurse.getLoc() == 10 || pg.getLoc() == -1
				|| admin.getLoc() == 33) {
			login.setRole("");
			c.show(getContentPane(), "login");
			connect = new ConnectGTD();
			//au = new AddUserGUI();
			//edit = new EditPatientGUI();
			//login = new LoginGUI();
			//doctor = new DoctorGUI();
			//nurse = new NurseGUI();
			//admin = new AdminGUI();
			//Do = new DoctorsOrdersGUI();
			//hh = new HealthHistoryGUI();
			//sp = new SearchPatientGUI();
			//np = new NewPatientGUI();
			//tr = new TreatmentRecordGUI();
			//va = new ViewAppointmentsGUI();
			//su = new SearchUserGUI();
			//eu = new EditUserGUI();
			//pg = new PatientGUI();
			//sa = new SearchAppointments();
			//id = new InputDoctorsOrdersGUI();
		}else if (sp.getLoc() == 6) {
			connect.setUser(sp.getUsern());
			tr.setUser(connect.getUser());
			c.show(getContentPane(), "tr");
		}else if (sp.getLoc() == 7 && login.getRole() == "doctor") {
			c.show(getContentPane(), "id");
		}else if (su.getLoc() == 12) {
			eu.setUser(su.getUsern());
			eu.act();
			c.show(getContentPane(), "eu");
		}else if (tr.getLoc() == 20 && login.getRole() == "doctor") {
			id.setTreatmentRecord(tr.getTreatmentRecord());
			connect.setTreatmentRecord(tr.getTreatmentRecord());
			c.show(getContentPane(), "id");
		}else if (su.getLoc() == 3) {
			connect.setNames(su.getUser());
		    su.setUsers(connect.getUserSearch());
			su.work();
			su.setUsers(connect.getUsers());
		}else if (tr.getLoc() == 1) {
			connect.setUser(tr.getUser());
			connect.editUser();
			tr.setLoc(-1);}
			
			if (edit.getLoc() == -4) {
				connect.setUser(sp.getUsern());
				connect.deleteUser();
				edit.setLoc(-1);
			}

			if (eu.getLoc() == -4) {
				connect.setUser(eu.getUser());
				connect.deleteUser();
				eu.setLoc(-1);
			}
			
		else if (np.getLoc() == -1 || hh.getLoc() == -1 || Do.getLoc() == -1
				|| eu.getLoc() == -1 || su.getLoc() == -1 || au.getLoc() == -1
				|| sa.getLoc() == -1 || edit.getLoc() == -1
				|| va.getLoc() == 21 || sp.getLoc() == -1 || tr.getLoc() == -1
				|| id.getLoc() == -1 || str.getLoc() == -1)

		{
			if (login.getRole() == "doctor") {
				c.show(getContentPane(), "doctor");
				doctor.setLoc(1);
			}

			if (login.getRole() == "nurse") {
				c.show(getContentPane(), "nurse");
				nurse.setLoc(1);
			}
			if (login.getRole() == "admin") {
				c.show(getContentPane(), "admin");
				admin.setLoc(1);
			}
			if (login.getRole() == "patient") {
				c.show(getContentPane(), "pg");
				pg.setLoc(1);
			}
			np.setLoc(0);
			sp.setLoc(0);

			

		}else if (doctor.getLoc() == 13 || nurse.getLoc() == 13
				|| admin.getLoc() == 13) {
			c.show(getContentPane(), "np");
		}else if (login.getLoc() == 1 && login.getRole() == "doctor") {
			connect.setUsername(login.name());
			connect.setPassWord(login.pass());
			connect.setCurrentUser(connect.login(login.getRole()));
			//if (connect.getUser().getRoles()[2] == true) {
			c.show(getContentPane(), "doctor");
			//}
		}else if (login.getRole() == "nurse" && login.getLoc() == 1)
		{

			connect.setUsername(login.name());
			connect.setPassWord(login.pass());
			connect.setCurrentUser(connect.login(login.getRole()));



			c.show(getContentPane(), "nurse");
		}else if (login.getRole() == "admin" && login.getLoc() == 1) {
			connect.setUsername(login.name());
			connect.setPassWord(login.pass());
			connect.setCurrentUser(connect.login(login.getRole()));
			//if (connect.getUser().getRoles()[3] == true) {
			c.show(getContentPane(), "admin");
			//}
		}else if (pg.getLoc() == 5) {
			Do.setUser(connect.getCurrentUser());
			c.show(getContentPane(), "Do");
		}else if (doctor.getLoc() == 2 || nurse.getLoc() == 2 || admin.getLoc() == 2) {
			
			c.show(getContentPane(), "sp");
		}else if (id.getLoc() == 1) {
			connect.getUser().addTreatmentRecord(id.getTreatmentRecord());
			connect.editUser();
			id.setLoc(-1);
		}else if (doctor.getLoc() == 3) {
			c.show(getContentPane(), "Do");
		}else if (doctor.getLoc() == 4) {
			c.show(getContentPane(), "hh");
		}else if (sp.getLoc() == 12) {
			doctor.setLoc(0);
			edit.setUser(connect.getUser());
			edit.act();
			c.show(getContentPane(), "EditP");
		}else if (edit.getLoc() == 3) {
			System.out.println(edit.getUser().getPharmacy());
			connect.setUser(edit.getUser());
			connect.editUser();
			edit.setLoc(-1);
		}else if (hh.getLoc() == 14) {
			c.show(getContentPane(), "sp");
		}else if (doctor.getLoc() == 5 || nurse.getLoc() == 5 || admin.getLoc() == 5) {
			c.show(getContentPane(), "tr");
		}else if (doctor.getLoc() == 6 || nurse.getLoc() == 6 || admin.getLoc() == 6) {
			c.show(getContentPane(), "va");
		}else if (admin.getLoc() == 8) {
			c.show(getContentPane(), "su");
		}else if (su.getLoc() == 7) {
			eu.setUser(connect.getUser());
			eu.act();
			c.show(getContentPane(), "eu");
		}else if (sp.getLoc() == 20) {
			connect.setUser(sp.getUsern());
			edit.setUser(sp.getUsern());
			edit.act();
			c.show(getContentPane(), "EditP");
		}
		else if (admin.getLoc() == 12) {
			c.show(getContentPane(), "au");}
			
			if (str.getLoc() == 3){
				str.setTreatmentRecords(connect.getCurrentUser());
			}

		else if (pg.getLoc() == 20) {
			
			c.show(getContentPane(), "str");
		}else if (au.getLoc() == 14) {
			connect.setUser(au.getUser());
			connect.addUser();

			c.show(getContentPane(), "admin");

		}else if (sp.getLoc() == 3) {
			connect.setNames(sp.getUser());
		    sp.setUsers(connect.getUserSearch());
			sp.work();
			sp.setUsers(connect.getUsers());
		}else if (np.getLoc() == 14) {
			connect.setUser(np.getUser());
			connect.addUser();
			if (login.getRole() == "doctor") {
				c.show(getContentPane(), "doctor");
			} else {
				c.show(getContentPane(), "nurse");
			}

		}else if (va.getLoc() == 12) {
			connect.setDate(va.getDate());
			va.setAppointments(connect.getOwnAppointments());
			va.setAppointments(connect.getAppointments());
		}else if (sa.getLoc() == -3) {
			connect.setDate(sa.getDate());
			sa.setApp(connect.getAppointmentsSearch());
			sa.run();
			// sa.setApp(connect.getAppointments());
		}else if(str.getLoc() ==3){
			str.setTreatmentRecords(connect.getCurrentUser());
		}else if (sa.getLoc() == 20) {
			connect.makeApp(sa.getAppointment());
		}else if (hh.getLoc() == 2) {
			hh.setUser(connect.getCurrentUser());
			hh.work();
		}else if (Do.getLoc() == -2) {
			Do.setUser(connect.getUser());
		}

	}

	public LoginGUI getLogin() {
		return login;
	}

	public DoctorGUI getDoc() {
		return doctor;
	}

	public void reset() {
		edit.setLoc(0);
		doctor.setLoc(0);
		login.setLoc(0);
		nurse.setLoc(0);
		admin.setLoc(0);
		Do.setLoc(0);
		hh.setLoc(0);
		sp.setLoc(0);
		np.setLoc(0);
		va.setLoc(0);
		tr.setLoc(0);
		su.setLoc(0);
		eu.setLoc(0);
		pg.setLoc(0);
		sa.setLoc(0);
		id.setLoc(0);
		au.setLoc(0);
		str.setLoc(0);

	}

}
